import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xindong/common/xindong_all.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/common/xindong_methods.dart';
import 'package:xindong/common/xindong_types.dart';
import 'package:xindong/manager/xindong_page_singleton.dart';
import 'package:xindong/routes/account/view/xindong_edit_sex_view.dart';
import 'package:xindong/routes/account/view/xindong_edit_text_view.dart';
import 'package:xindong/routes/common/xindong_input_text_yemian.dart';
import 'package:xindong/utils/xindong_text_util.dart';
import 'package:xindong/widgets/common/xindong_app_bar.dart';
import 'package:xindong/widgets/common/xindong_button.dart';
import 'package:xindong/widgets/dialog/xindong_birthday_sheet.dart';
import 'package:xindong/widgets/xindong_avatar_upload_widget.dart';

import 'controller/xindong_register_ctr.dart';

class XinDongRegisterYeMian extends StatefulWidget {
  const XinDongRegisterYeMian({super.key});

  @override
  State<XinDongRegisterYeMian> createState() => _XinDongRegisterYeMianState();
}

class _XinDongRegisterYeMianState extends State<XinDongRegisterYeMian> {
  Future<void> onTap({required String title, required XinDongRegisterCtr controller}) async {
    switch (title) {
      case '昵称':
        {
          XinDongPageSingleton.instance.pushYeMian(XinDongInputTextYeMian(
            inputEnum: XinDongInputProfileEnum.name,
            value: controller.registerClass.name,
            doneCallback: (String text) {
              controller.registerClass.name = text;
              controller.update();
            },
          ));
        }
        break;
      case '邀请码':
        {
          if (!TextUtil.isEmpty(controller.registerClass.send_code)) {
            showToast("不可修改");
            return;
          }
          XinDongPageSingleton.instance.pushYeMian(XinDongInputTextYeMian(
            inputEnum: XinDongInputProfileEnum.inviteCode,
            value: controller.registerClass.send_code,
            doneCallback: (String text) {
              controller.registerClass.send_code = text;
              controller.update();
            },
          ));
        }
        break;
      case '生日':
        XinDongBirthdaySheet.show(
          isMan: controller.registerClass.sex == Gender.male,
          selectedDateText: controller.registerClass.birthday,
          doneCallback: (text) {
            controller.registerClass.birthday = text;
            controller.update();
          },
        );
        break;
    }
  }

  @override
  void initState() {
    super.initState();
    XinDongRegisterCtr.to.registerClass.send_code = XinDongAll.instance.inviteCode ?? "";
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: XinDongAppBar("注册"),
      body: GetBuilder<XinDongRegisterCtr>(builder: (controller) {
        return Stack(
          children: [
            ListView(
              children: [
                Container(
                  alignment: Alignment.center,
                  padding: const EdgeInsets.only(top: 23),
                  child: XinDongAvatarUploadWidget(
                    avatarFile: controller.avatarFile,
                    avatarUrl: controller.registerClass.head,
                    callBack: (file) {
                      controller.avatarFile = file;
                      controller.update();
                    },
                  ),
                ),
                const SizedBox(height: 24),
                const XinDongEditGenderView(),
                const SizedBox(height: 24),
                _buildItem(title: '昵称', hint: '输入昵称', content: controller.registerClass.name, controller: controller),
                _buildItem(
                    title: '生日', hint: '选择生日', content: controller.registerClass.birthday, controller: controller),
                _buildItem(
                    title: '邀请码',
                    hint: '输入邀请码（选填）',
                    content: controller.registerClass.send_code,
                    controller: controller),
                const SizedBox(height: 120),
              ],
            ),
            Positioned(
              bottom: 48,
              left: 16,
              right: 16,
              child: SizedBox(
                height: 48,
                width: screenWidth,
                child: XinDongCommonButton("开始使用app", onTap: () {
                  controller.startRegister();
                }),
              ),
            ),
          ],
        );
      }),
    );
  }

  Widget _buildItem(
      {required String title, required String? content, required String hint, required XinDongRegisterCtr controller}) {
    return XinDongEditTextView(
      title: title,
      content: content,
      hint: hint,
      onTap: () => onTap(title: title, controller: controller),
    );
  }
}
